//
//  PGTopicListTableViewController.m
//  PhotographicGuide
//
//  Created by indream on 13-4-12.
//  Copyright (c) 2013年 indream. All rights reserved.
//

#import "PGTopicListTableViewController.h"
#import "CKRefreshControl.h"
#import "PGWebViewController.h"
#import "PGDetailViewController.h"
#import "PGTopicManager.h"

static NSString* NOTIFICATION_TOPIC_TABLE_REFRESH = @"__NOTIFICATION_TOPIC_TABLE_REFRESH__";

@interface PGTopicListTableViewController ()

@end

@implementation PGTopicListTableViewController

-(id)init{
    self = [super init];
    if(self){
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadTable:) name:NOTIFICATION_TOPIC_TABLE_REFRESH object:nil];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    //self.refreshControl = [[UIRefreshControl alloc] init];
    //[self.refreshControl addTarget:self action:@selector(doRefresh:) forControlEvents:UIControlEventValueChanged];
     [self doRefresh:nil];
}
-(void)viewWillAppear:(BOOL)animated{
   
    [super viewWillAppear:animated];
}
-(void)viewDidUnload{
    [super viewDidUnload];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NOTIFICATION_TOPIC_TABLE_REFRESH object:nil];
}
- (void)doRefresh:(CKRefreshControl *)sender {
    [self.tableView reloadData];
    [[PGTopicManager sharedManager]requestWithTag:self.tag];
}
-(void)reloadTable:(NSNotification*)notification{
    [self.tableView reloadData];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([PGTopicManager sharedManager].list){
        return [PGTopicManager sharedManager].list.count;
    }
    return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([PGTopicManager sharedManager].list){
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            return 236;
        }else{
            return 491;
        }
    }return 54.0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PGViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PGViewCell"];
    if (cell == nil) {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            [[NSBundle mainBundle] loadNibNamed:@"PGViewCell" owner:self options:nil];
        }else{
            [[NSBundle mainBundle] loadNibNamed:@"PGViewCell_iPad" owner:self options:nil];
        }
        cell = _tvCell;
        self.tvCell = nil;
    }
    cell.iconView.image = nil;
    cell.thumbView.image = [UIImage imageNamed:@"home_default_image.png"];
    [cell loadImage:[[[PGTopicManager sharedManager].list objectAtIndex:indexPath.row] objectForKey:@"imageSmall"]];
    cell.titleLabel.text = [[[PGTopicManager sharedManager].list objectAtIndex:indexPath.row] objectForKey:@"name"];
    [cell updateDate:[[[[PGTopicManager sharedManager].list objectAtIndex:indexPath.row] objectForKey:@"time"]intValue]];
    
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *article = [[PGTopicManager sharedManager].list objectAtIndex:indexPath.row];
    NSString *url = [article objectForKey:@"url"];
    if([url componentsSeparatedByString:@"http://"].count>1){
        PGWebViewController *controller = [[PGWebViewController alloc]init];
        controller.url = url;
        controller.title = [article objectForKey:@"name"];
        [self.navigationController pushViewController:controller animated:YES];
    }else{
        PGDetailViewController *controller = [[PGDetailViewController alloc]initWithNibName:[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone?@"PGDetailViewController":@"PGDetailViewController_iPad" bundle:nil];
        controller.articleId = [[article objectForKey:@"id"] intValue];
        [self.navigationController pushViewController:controller animated:YES];
    }
}
-(void)doFinishLoadMore{
    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
